package model.board;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import model.board.paging.Board_ListVO;
import model.board.paging.CommonConstants;
import model.board.paging.ListVO;
import model.board.paging.PagingBean;

public class FreeBoardService {
	public FreeBoardDao freeBoardDao;

	public FreeBoardService() {
		super();
	}

	public FreeBoardService(FreeBoardDao freeBoardDao) {
		super();
		this.freeBoardDao = freeBoardDao;
	}
	/**
	 *	자유게시판 글쓰기 
	 */
	public void freeWrite(FreeBoardVO bvo) throws SQLException {
		System.out.println("service freeWrite bvo:"+bvo);
		int no=freeBoardDao.freeWrite(bvo);
		System.out.println("service freeWrite no:"+no);
		String date=freeBoardDao.selectByNoForDate(no);
		bvo.setB_date(date);
	}
	public HashMap freeAllList(String pageNo) throws SQLException {
		if(pageNo==null||pageNo=="")
			pageNo="1";
		int numberPerPage=CommonConstants.FREE_CONTENT_NUMBER_PER_PAGE;
		HashMap map=new HashMap();
		map.put("pageNo", pageNo);
		map.put("numberPerPage", numberPerPage); 
		List<FreeBoardVO> list=freeBoardDao.getContentList(map);
		int total=freeBoardDao.totalContent();
		PagingBean pagingBean= new PagingBean(total, Integer.parseInt(pageNo));
		ListVO lvo=new ListVO(list,pagingBean);
		//오늘날짜를 받아올수 있는 객체
		Date date = new Date();
		//날짜를 원하는 포멧으로 받을 수 있는 객체 DB에 yyyy.mm.dd로 저장되어 있으므로 아래와 같이 포멧을 맞춤
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		//맵 객체를 생성하여 map.put을 이용 오늘날짜를 담음
		map.put("today", df.format(date));
		//map.put을 이용 LisvVO를 담음
		map.put("lvo", lvo);
		return map;
	}
	public FreeBoardVO showContent(int bno) throws SQLException{
		freeBoardDao.updateCount(bno);
		return freeBoardDao.showContent(bno);
	}
	public FreeBoardVO showContentNoCnt(int bno1) throws SQLException {
		System.out.println("service showContentNoCnt"+bno1);
		return freeBoardDao.showContent(bno1); 
	} 
	public void deleteContent(int bno) throws SQLException {
		freeBoardDao.deleteContent(bno);
	}
	public void freeUpdateContent(FreeBoardVO bvo) throws SQLException {
		freeBoardDao.freeUpdateContent(bvo);
	}

	public void freeDeleteFile(String newfilename) {
		File f=new File(newfilename);
	}
 
	public int freeReplyWrite(FreeBoardVO fvo) throws SQLException {
		int ref=fvo.getB_ref();
		int restep=fvo.getB_restep();
		int relevel=fvo.getB_relevel();
		freeBoardDao.freeUpdateRestep(ref,restep);
		fvo.setB_restep(restep+1);
		fvo.setB_relevel(relevel+1);
		int num=freeBoardDao.freeInsertRefContent(fvo);
		return num;
	}

	public Board_ListVO freeSearch(HashMap map) throws SQLException {
		String pageNo=(String)map.get("pageNo");
		FreeBoardVO vo=(FreeBoardVO)map.get("bvo");
		System.out.println("service searchFree vo:"+vo);
		if(pageNo==null||pageNo=="")
			pageNo="1";
		int numberPerPage=CommonConstants.FREE_CONTENT_NUMBER_PER_PAGE;
		map.put("pageNo", pageNo);
		map.put("numberPerPage", numberPerPage);
		map.put("b_title", vo.getB_title());
		map.put("b_writer", vo.getB_writer());
		System.out.println("service searchFree map:"+map);
		List list=freeBoardDao.freeSearch(map);
		System.out.println("service searchQna list:"+list);
		int total=freeBoardDao.freeTotalContentBySearch(vo);
		PagingBean paging=new PagingBean(total,Integer.parseInt(pageNo));
		Board_ListVO lvo=new Board_ListVO(list,paging,map);
		System.out.println("service searchFree lvo:"+lvo);
		return lvo;
	}
	public void freeInsertReply(BoardReplyVO brvo) throws SQLException {
		int no=freeBoardDao.freeInsertReply(brvo);
		String date=freeBoardDao.selectByNoForDate(no);
		freeBoardDao.updateReplyCount(brvo.getB_no());
		brvo.setBoard_date(date);
	} 
	
	public List freeAllReply(int bno1) throws SQLException { 
		return freeBoardDao.freeAllReply(bno1);
	}

	public void freeDelReply(HashMap bmap) throws SQLException {
		System.out.println("11");
		freeBoardDao.freeDelReply((String) bmap.get("board_no"));
		System.out.println("22"+bmap.get("bno"));
		freeBoardDao.deleteReplyCount((Integer)bmap.get("bno"));
	}
	//관리자가 글 삭제시 그 안의 리플까지 같이 삭제한다.
		public void deleteRe(int bno) throws SQLException{
			freeBoardDao.deleteRef(bno);
		}

/*	public int getTotalReply(int b_no) throws SQLException {
		return freeBoardDao.getTotalReply(b_no);
	}*/
}
